package com.sky.zzh.home;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

import com.next.intf.ITaskListener;
import com.next.net.SHGet;
import com.next.net.SHPost;
import com.next.net.SHTask;
import com.sky.widget.SHDialog;
import com.sky.widget.SHToast;
import com.sky.widget.TimerButton;
import com.sky.widget.sweetdialog.SweetDialog;
import com.sky.zzh.R;
import com.sky.zzh.SHApplication;
import com.sky.zzh.util.CommonUtil;
import com.sky.zzh.util.ConfigDefinition;
import com.sky.zzh.util.UserInfoManager;
import com.sky.zzh.view.SHEditTextNoClear;

/**
 * 获取验证码
 * 
 * @author skypan
 * 
 */
public class GetCodeActivity extends Activity implements ITaskListener{

	private Button mBtnNext,  mBtnBack2;
	private TimerButton mBtnGet;
	private ImageView mBtnBack;
	private SHEditTextNoClear mEtPhone,mEtCode;
	private int code;//验证码
	private SHPost registerTask;//注册请求

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_get_code);
		mBtnBack = (ImageView) findViewById(R.id.btn_back);
		mBtnBack2 = (Button) findViewById(R.id.btn_back2);
		mBtnNext = (Button) findViewById(R.id.btn_next);
		mBtnGet = (TimerButton) findViewById(R.id.btn_get);
		
		mEtPhone = (SHEditTextNoClear) findViewById(R.id.et_username);
		mEtCode = (SHEditTextNoClear) findViewById(R.id.et_password);

		setListeners();
		SHApplication.getInstance().addActivity(this);
	}

	private void setListeners() {
		mBtnBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});

		mBtnBack2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		mBtnGet.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(CommonUtil.isEmpty(mEtPhone.getText().toString().trim())){
					SHToast.showToast(GetCodeActivity.this, "请输入手机号码");
					return;
				}
				
				mBtnGet.start();
				
				SHDialog.ShowProgressDiaolg(GetCodeActivity.this);
				code = (int) (Math.random()*9000+1000);
				String msg = "您的验证码是"+code;
				SHGet post = new SHGet();
				post.setUrl("http://sdk4report.eucp.b2m.cn:8080/sdkproxy/sendsms.action?cdkey=6SDK-EMY-6688-KGVMO&password=585015&phone="+mEtPhone.getText().toString().trim()+"&message="+msg+"&addserial=10010");
//				post.getTaskArgs().put("cdkey", "6SDK-EMY-6688-KGVMO");
//				post.getTaskArgs().put("password", "585015");
//				post.getTaskArgs().put("phone", mEtPhone.getText().toString().trim());
//				post.getTaskArgs().put("message", msg);
//				post.getTaskArgs().put("addserial", "10010");
				post.setListener(GetCodeActivity.this);
				post.start();
			}
		});
		
		mBtnNext.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				if("meddo99.com".equals(getIntent().getStringExtra("loginType"))){
					if(CommonUtil.isEmpty(mEtPhone.getText().toString().trim())){
						SHToast.showToast(GetCodeActivity.this, "请输入手机号码");
						return;
					}
					if(CommonUtil.isEmpty(mEtCode.getText().toString().trim())){
						SHToast.showToast(GetCodeActivity.this, "请输入验证码");
						return;
					}
					System.out.println(mEtCode.getText().toString().trim()+","+code);
					if(!mEtCode.getText().toString().trim().equals(String.valueOf(code))){
						SHToast.showToast(GetCodeActivity.this, "验证码不正确");
						return;
					}
					
					Intent intent = new Intent(GetCodeActivity.this,RegisterActivity.class);
					intent.putExtra("phone", mEtPhone.getText().toString().trim());
					intent.putExtra("loginType", getIntent().getStringExtra("loginType"));
					intent.putExtra("openid", "".equals(getIntent().getStringExtra("openid"))?mEtPhone.getText().toString().trim():getIntent().getStringExtra("openid"));
					startActivityForResult(intent, 0);
				}else{
					requestRegister();
				}
				
			}
		});
	}

	private void requestRegister(){
		SHDialog.ShowProgressDiaolg(this);
		registerTask = new SHPost();
		registerTask.setUrl(ConfigDefinition.URL+"user/UserRegister");
		registerTask.setListener(this);
		registerTask.getTaskArgs().put("UserValidationServer", getIntent().getStringExtra("loginType"));
		registerTask.getTaskArgs().put("UserIdOriginal", getIntent().getStringExtra("openid"));
//		registerTask.getTaskArgs().put("UserIdOriginal", "123456");
		registerTask.getTaskArgs().put("CellPhone", getIntent().getStringExtra("phone"));
		registerTask.getTaskArgs().put("Email", "");
		registerTask.getTaskArgs().put("Password", "");
		registerTask.start();
	}
	
	@Override
	public void onTaskFinished(SHTask task) throws Exception {
		// TODO Auto-generated method stub
		SHDialog.dismissProgressDiaolg();
		System.out.println(task.getResult());
		System.out.println(task.getResult().toString());
		JSONObject json = new JSONObject(task.getResult().toString());
		if ("200".equals(json.optString("ReturnCode"))) {
			UserInfoManager.getInstance().setUserId(json.optString("UserID"));
			UserInfoManager.getInstance().sync(this, true);
			Intent intent = new Intent(GetCodeActivity.this,ZZHMainActivity.class);
			startActivity(intent);
			finish();
		}else if("10005".equals(json.optString("ReturnCode"))){
			new SweetDialog(GetCodeActivity.this, SweetDialog.ERROR_TYPE).setTitleText("提示").setContentText("用户已存在").show();
		}else{
			new SweetDialog(GetCodeActivity.this, SweetDialog.ERROR_TYPE).setTitleText("提示").setContentText(json.optString("ErrorMessage")).show();
		}
	}

	@Override
	public void onTaskFailed(SHTask task) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTaskUpdateProgress(SHTask task, int count, int total) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTaskTry(SHTask task) {
		// TODO Auto-generated method stub
		
	}

}
